<?php
    class AdminDthink_Model_News extends Zend_Db_Table_Abstract
    {
        protected $_name = 'news';
        protected $_primary = 'news_id';
        protected $_db;
        protected $_fullToday;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
            $this->_fullToday = date('Y-m-d H:i:s');
        }
        
        public function listNews()
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('news_id DESC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function checkNews($news_title, $news_id = '')
        {
            if($news_id != '')
            {
                $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('news_title = ?', $news_title)
                            ->where('news_id = ?', $news_id);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
            else
            {
                $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('news_title = ?', $news_title);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
        }
        
        public function getNewsById($news_id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('news_id = ?', $news_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function addNews($cat_id, $news_title, $news_image, $news_content, $news_plaintext)
        {
            $data = array(
                'cat_id' => $cat_id,
                'news_title' => $news_title,
                'news_image' => $news_image,
                'news_content' => $news_content,
                'news_plaintext' => $news_plaintext,
                'status' => 0,
                'create_date' => $this->_fullToday,
            );
            $result = $this->_db->insert($this->_name, $data);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public function editNews($news_id, $cat_id, $news_title, $news_image = '', $news_content, $news_plaintext, $status)
        {
            $where = 'news_id = ' . $news_id;
            if($news_image != '')
            {
                $data = array(
                    'cat_id' => $cat_id,
                    'news_title' => $news_title,
                    'news_image' => $news_image,
                    'news_content' => $news_content,
                    'news_plaintext' => $news_plaintext,
                    'status' => $status,
                );
                $result = $this->_db->update($this->_name, $data, $where);
                if($result)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                $data = array(
                    'cat_id' => $cat_id,
                    'news_title' => $news_title,
                    'news_content' => $news_content,
                    'news_plaintext' => $news_plaintext,
                    'status' => $status,
                );
                $result = $this->_db->update($this->_name, $data, $where);
                if($result)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        
        public function deleteNews($news_id)
        {
            $where = 'news_id = ' . $news_id;
			$query = $this->_db->select()
                            ->from($this->_name)
							->where($where);
			$row = $this->_db->fetchRow($query);
			if (is_file(PUBLIC_PATH . '/files/news/' . $row['news_image']))
			{
				unlink(PUBLIC_PATH . '/files/news/' . $row['news_image']);
			}
			$result = $this->_db->delete($this->_name, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
        }
        
        public function deleteManyNews($listId)
        {
            foreach ($listId as $value)
			{
				$where = 'news_id = ' . $value;
				$query = $this->_db->select()
							->from($this->_name)
							->where($where);
				$row = $this->_db->fetchRow($query);
				if (is_file(PUBLIC_PATH . '/files/news/' . $row['news_image']))
				{
					unlink(PUBLIC_PATH . '/files/news/' . $row['news_image']);
				}
				$result = $this->_db->delete($this->_name, $where);
			}
        }
        
        public function searchNews($keyword)
        {
            $where = "MATCH(news_title, news_plaintext) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where($where);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
    }
